/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import model.Game;
import model.Game.Difficulties;
import model.LevelSpecification;


/**
 *
 * @author meije_000
 */
public class GamePaneel extends javax.swing.JPanel {

    private Game game;
    HoofdFrame hoofdframe;
    /**
     * Creates new form GamePanel
     */
    public GamePaneel(LevelSpecification level,HoofdFrame frame) {
        initComponents();
        
        this.game = new Game(pnlField, this, level);
        this.addKeyListener(game);
        this.game.draw();
        hoofdframe = frame;

        cboDifficulty.setSelectedItem(level.getDifficulty());
        //this.setTitle("Level: " + level.getName());
    }
    
    private GamePaneel() {
    }
    
    public void messageDialog(String message) {
        JOptionPane.showMessageDialog(this, message);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnPauze = new javax.swing.JButton();
        pnlField = new view.FieldPanel();
        btnStop = new javax.swing.JButton();
        btnStart = new javax.swing.JButton();
        Back = new javax.swing.JButton();

        btnPauze.setText("Pauze");
        btnPauze.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPauzeActionPerformed(evt);
            }
        });

        cboDifficulty.setModel(new DefaultComboBoxModel<>(Difficulties.values()));
        cboDifficulty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboDifficultyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlFieldLayout = new javax.swing.GroupLayout(pnlField);
        pnlField.setLayout(pnlFieldLayout);
        pnlFieldLayout.setHorizontalGroup(
            pnlFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        pnlFieldLayout.setVerticalGroup(
            pnlFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );

        btnStop.setText("Stop");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        Back.setText("Back");
        Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnStop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnPauze, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(cboDifficulty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(Back)
                    .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Back)
                        .addGap(38, 38, 38)
                        .addComponent(btnStart)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStop)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPauze)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboDifficulty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPauzeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPauzeActionPerformed
        this.game.stop();
        this.requestFocus();
    }//GEN-LAST:event_btnPauzeActionPerformed

    private void cboDifficultyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboDifficultyActionPerformed
        Difficulties difficulty = (Game.Difficulties) cboDifficulty.getModel().getSelectedItem();
        this.game.setDifficulty(difficulty);
        this.requestFocus();
    }//GEN-LAST:event_cboDifficultyActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        this.game.pauze();
        hoofdframe.setContentPanel(new LevelSelectionPaneel(hoofdframe));
        //this.dispose();
        //dit moet aangepast worden.
    }//GEN-LAST:event_btnStopActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        this.game.start();
        this.requestFocus();
    }//GEN-LAST:event_btnStartActionPerformed

    private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
            /*
             * maak een nieuwe LevelSelectionPaneel aan, Geef de instance van het hoofdframe mee.
             * Run uit instance hoofdframe setContentPanel om het nieuwe LevelSelectionPaneel in het frame te zetten
             */
            LevelSelectionPaneel levelselection = new LevelSelectionPaneel(hoofdframe);
            hoofdframe.setContentPanel(levelselection);
    }//GEN-LAST:event_BackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back;
    private javax.swing.JButton btnPauze;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private final javax.swing.JComboBox cboDifficulty = new javax.swing.JComboBox<Difficulties>();
    private view.FieldPanel pnlField;
    // End of variables declaration//GEN-END:variables
}
